<?php

require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends MrolesWebPage {

    public function main(){

        global $_W,$_GPC;

        $token = getToken();
        include $this->template('mroles/order');
    }

    public function detail(){

        global $_W,$_GPC;
        $uid = $_W['manageid'];
        $type = $_GPC['type'];

        //验证令牌
        /*if(empty($_GPC['token']) || !checkToken($_GPC['token'])){
            show_json(0, '参数有误');
        }*/

//        $owner = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_goods_owner').' WHERE uid=:uid AND deleted=0', array(':uid'=>$uid));
//
//        $goodsid = array(0);
//        foreach ($owner as $item){
//            $goodsid[] = $item['goods_id'];
//        }

//        $goodsid = implode(',', $goodsid);

        /*$goodsid = array(0);
        if($_W['isowner']){
            $goodsid = $this->getOwnerGoods();
        }

        //店主设备id
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods();
            $goodsid = array_merge($goodsid, $mdids);
            unset($mdids);
        }

        //服务商设备id
        if($_W['ischannel']){
            $chids = $this->getMendianGoods(2);
            $goodsid = array_merge($goodsid, $chids);
            unset($chids);
        }

        if($_W['isoperator']){
            $opids = $this->getOperatorGoods();
            $goodsid = array_merge($goodsid, $opids);
            unset($opids);
        }

        $goodsid = implode(',', array_unique($goodsid));
        $goodsid = empty($goodsid) ? 0 : $goodsid;

        $condition = '';
        $pageparams = $params = array();

        if(!empty($_GPC['status'])){
            $condition .= ' AND o.status=:status';
            $params[':status'] = intval($_GPC['status']);
        }

        if(!empty($_GPC['keyword'])){

            $type = $_GPC['type'];
            if($type == 'ordersn'){
                $condition .= ' AND locate(:keyword,o.ordersn)>0 ';
            }else if($type == 'goods'){
                $condition .= ' AND locate(:keyword,o.goods_name)>0 ';
            }else if($type == 'username'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0 ';
            }else{
                $condition .= ' AND locate(:keyword,o.mendian_name)>0 ';
            }
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            $pageparams['type'] = $type;
            $pageparams['keyword'] = $_GPC['keyword'];
        }

        $searchtime = trim($_GPC['searchtime']);
        if (!(empty($searchtime)) && is_array($_GPC['time']) && in_array($searchtime, array('create', 'pay', 'send', 'finish')))
        {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
            $condition .= ' AND o.' . $searchtime . 'time >= :starttime AND o.' . $searchtime . 'time <= :endtime ';
            $params[':starttime'] = $starttime;
            $params[':endtime'] = $endtime;
            $pageparams['searchtime'] = $searchtime;
            $pageparams['time[start]'] = $_GPC['time']['start'];
            $pageparams['time[end]'] = $_GPC['time']['end'];

        }
        if (empty($starttime) || empty($endtime))
        {
            $starttime = strtotime('-1 month');
            $endtime = time();
        }

        $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $psize = 10;

        $list = pdo_fetchall('SELECT o.*, g.name as goods, m.avatar, m.realname FROM '.tablename('mx_shop_service_order').' o 
                                    LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id 
                                    LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid 
                                    WHERE o.goods_id in ('.$goodsid.') '.$condition.' ORDER BY id DESC LIMIT '.($pindex - 1) * $psize . ','. $psize, $params);

        $total = pdo_fetchcolumn('SELECT count(o.id) FROM '.tablename('mx_shop_service_order').' o LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid WHERE o.goods_id in ('.$goodsid.') '.$condition, $params);

        $pager = owpager($total, $pindex, $psize, $pageparams);

        $orderstatus = array(1 => array('css' => 'danger', 'name' => '待付款'), 2 => array('css' => 'warning', 'name' => '进行中'), 3 => array('css' => 'success', 'name' => '已完成'), 4 => array('css' => 'default', 'name' => '已关闭'), 5 => array('css' => 'danger', 'name' => '维权申请'), 6 => array('css' => 'default', 'name' => '维权完成',));*/
        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = ' o.deleted=0';

        $paras = array();

        $orderstatus = array(1 => array('css' => 'danger', 'name' => '待付款'), 2 => array('css' => 'warning', 'name' => '进行中'), 3 => array('css' => 'success', 'name' => '已完成'), 4 => array('css' => 'default', 'name' => '已关闭'), 5 => array('css' => 'danger', 'name' => '维权申请'), 6 => array('css' => 'default', 'name' => '维权完成',));

        if($type != 0 || !empty($type)){
            $condition .= ' AND o.status = :status';
            $paras[':status'] = $type;
        }

        $paytype_list = array('3'=>array('name'=>'余额'), '24'=>array('name'=>'微信'), '38'=>array('name'=>'支付宝'));

        $paytype = intval($_GPC['paytype']);
        if(!empty($type)){
            $condition .= ' AND o.paytype = :paytype';
            $paras[':paytype'] = $paytype;
        }

        if (empty($starttime) || empty($endtime))
        {
            $starttime = strtotime('-1 month');
            $endtime = time();
        }
        $searchtime = trim($_GPC['searchtime']);
        if (!(empty($searchtime)) && is_array($_GPC['time']) && in_array($searchtime, array('create', 'pay', 'send', 'finish')))
        {
            $starttime = strtotime($_GPC['time']['start']);
            $endtime = strtotime($_GPC['time']['end']);
            $condition .= ' AND o.' . $searchtime . 'time >= :starttime AND o.' . $searchtime . 'time <= :endtime ';
            $paras[':starttime'] = $starttime;
            $paras[':endtime'] = $endtime;
        }

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if($_GPC['searchfield'] == 'ordersn'){
                $condition .= ' AND locate(:keyword,o.ordersn)>0';

            } else if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0)';

            } else if ($_GPC['searchfield'] == 'goods'){
                $condition .= ' AND locate(:keyword,o.goods_name)>0 ';

            }else if ($_GPC['searchfield'] == 'mendian'){
                $condition .= ' AND locate(:keyword,o.mendian_name)>0 ';
            }
        }
        $list_sql = 'SELECT o.* ,m.realname as realname, r.reason as reason, r.content as content,p.usetime as usetime FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id LEFT JOIN ' .tablename('mx_shop_service_goods_type_price') . ' p ON o.price_id=p.id LEFT JOIN '.tablename('mx_shop_service_order_refund').' r ON o.id=r.orderid WHERE ' . $condition . ' ORDER BY o.id DESC LIMIT '. (($pindex - 1) * $psize) . ',' . $psize;

        $list = pdo_fetchall($list_sql, $paras);

        $total = pdo_fetchcolumn('SELECT count(o.id) FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id WHERE ' . $condition, $paras);

        $money = pdo_fetchcolumn('SELECT sum(o.realprice) as money FROM ' . tablename('mx_shop_service_order') . ' o LEFT JOIN ' .tablename('mx_shop_user') . ' m ON o.uid=m.id WHERE ' . $condition, $paras);

        $pager = pagination($total, $pindex, $psize);
//        show_json('1',array('sql'=>$list_sql,'data'=>$list));die;
        include $this->template('mroles/order');
    }

    public function view(){

        global $_W,$_GPC;

        $id = intval($_GPC['id']);

        $order = pdo_fetch('SELECT o.*, m.realname, m.avatar, g.name as goods, g.docking_number, d.mendianname as mendian, d.tel, d.address, dm.nickname as manage, dm.avatar as manageavatar, b.name as brand, t.name as type '.
                                'FROM '.tablename('mx_shop_service_order').' o '.
                                'LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid '.
                                'LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=o.goods_id '.
                                'LEFT JOIN '.tablename('mx_shop_service_goods_brands').' b ON b.id=g.brands_id '.
                                'LEFT JOIN '.tablename('mx_shop_service_goods_type').' t ON t.id=g.type_id '.
                                'LEFT JOIN '.tablename('mx_shop_mendian').' d ON d.id=g.mendian_id '.
                                'LEFT JOIN '.tablename('mx_shop_user').' dm ON dm.id=d.manageid '.
                                'WHERE o.id=:id',
                                array(':id'=>$id)
                            );
        if(empty($id) || empty($order)){
            $this->message('订单不存在!');
        }

        $orderstatus = array(1 => array('css' => 'danger', 'name' => '待付款'), 2 => array('css' => 'warning', 'name' => '进行中'), 3 => array('css' => 'success', 'name' => '已完成'), 4 => array('css' => 'default', 'name' => '已关闭'), 5 => array('css' => 'danger', 'name' => '维权申请'), 6 => array('css' => 'default', 'name' => '维权完成',));

        $paytype = array('3'=>array('name'=>'余额'), '24'=>array('name'=>'微信'), '38'=>array('name'=>'支付宝'));

        include $this->template('mroles/order/view');
    }

}